package com.example.neusoft.cook.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.neusoft.cook.R;
import com.example.neusoft.cook.base.BaseFragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment {
    private TextView titleView;
    private RadioGroup radioGroup;

    private ShopListFragment shopListFragment;
    private FavorFragment favorFragment;
    private SearchFragment searchFragment;
    private MyFragment myFragment;

    private FragmentManager manager;
    private FragmentTransaction transaction;


    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        titleView = (TextView) view.findViewById(R.id.textView_title);
        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);

        //
        manager = getChildFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio_shop_list:
                        shopListFragment = new ShopListFragment();
                        replace(shopListFragment);
                        break;
                    case R.id.radio_favor:
                        favorFragment = new FavorFragment();
                        replace(favorFragment);
                        break;
                    case  R.id.radio_search:
                        searchFragment = new SearchFragment();
                        replace(searchFragment);
                        break;
                    case  R.id.radio_my:
                        myFragment = new MyFragment();
                        replace(myFragment);
                        break;
                }
            }
        });
        radioGroup. check(R.id.radio_shop_list);

    }

    private void replace(Fragment fragment){
        transaction = manager.beginTransaction();
        transaction.replace(R.id.frameLayout,fragment);
        transaction.commit();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

}
